<?php

/*

Adoption Center (adopt.php)

*/

$rank_check = 1;
$page_title = "Adoption Center";

if ($act == "disown2")
{
	include "global.inc.php";

	$findPet = fetch("SELECT * FROM user_pets2 WHERE owner = '$userid' AND adoption = '0' AND id = '$disown' AND game = '$game'");
	if (!$findPet[id])
	{
		die(header(error("adopt.php?game=$game&act=disown","That is not your pet!")));
	}
	$cost2adopt = rand(round(($findPet[level] * 100) * .7), round(($findPet[level] * 100) * 1.3));
	mysql_query("UPDATE user_pets2 SET adoption=1 WHERE owner = '$userid' AND adoption = '0' AND id = '$disown' AND game = '$game'");
	mysql_query("UPDATE user_pets2 SET cost2adopt=$cost2adopt WHERE owner = '$userid' AND adoption = '1' AND id = '$disown' AND game = '$game'");
	die(header(error("adopt.php?game=$game&act=disown","$findPet[name] has been disowned! :(")));
}


if ($act == "adopt3")
{
	include "header.inc.php";

	// Start Check

	$checking = "adoption";
	$limit1 = 3600;
	$limit = $timestamp - $limit1;

	$findCheck = fetch("SELECT * FROM checking2 WHERE userid = '$userid' AND check_what = '$checking' AND timestamp > '$limit' AND game = '$game'");

	if ($findCheck[id])
	{
		die("$openHTML<p>You can only adopt a pet every $limit1 seconds. <a href=$base_url/adopt.php?game=$game>Back</a></p>$closeHTML");
	}

	if (!$findCheck[id])
	{
		mysql_query("DELETE FROM checking2 WHERE userid = '$userid' AND check_what = '$checking' AND game = '$game'");
		mysql_query("INSERT INTO checking2 (userid,check_what,timestamp,game) VALUES ('$userid','$checking','$timestamp','$game')");
	}

	// End Check

	$findPet = fetch("SELECT * FROM user_pets2 WHERE adoption = '1' AND id = '$id' AND game = '$game'");
	if (!$findPet[id])
	{
		die(header(error("adopt.php?game=$game&act=adopt2&id=$id","That pet is not up for adoption!")));
	}
	if ($points < $findPet[cost2adopt])
	{
		die(header(error("adopt.php?game=$game&act=adopt2&id=$id","You do not have enough $pointsVar to adopt this pet.")));
	}
	points($game,"$username", "-$findPet[cost2adopt]");
	mysql_query("UPDATE user_pets2 SET adoption=0 WHERE id = '$id' AND game = '$game'");
	mysql_query("UPDATE user_pets2 SET owner=$userid WHERE id = '$id' AND game = '$game'");
	die(header(error("mypets.php?game=$game","You have adopted $findPet[name]!!!!")));
}

include "header.inc.php";

echo $openHTML;

print "<p align=center><a href=adopt.php?game=$game>Adoption Center</a> | <a href=adopt.php?game=$game&act=adopt>Adopt a Pet</a> | <a href=adopt.php?game=$game&act=disown>Disown a Pet</a> | <a href=mypets.php?game=$game>My Pets</a></p>";

if (!$act)
{
echo "<p>Welcome to the adoption center, here you can either look for pets to adopt, or put your pet up for adoption. So please choose what you want to do below.</p>";

ECHO <<<END

<P align=center>
<TABLE BGCOLOR="$tableBorder" CELLSPACING=1 CELLPADDING=0 WIDTH=300>
   <TR BGCOLOR="$topAndBottomBG">
      <TD HEIGHT=50>
         <CENTER><FONT SIZE="+3" COLOR="$topAndBottomText"><B><a href=adopt.php?game=$game&act=adopt class=white>Adopt a Pet</a></B></FONT></CENTER>
      </TD>
   </TR>
</TABLE>
</P>

<P align=center>
<TABLE BGCOLOR="$tableBorder" CELLSPACING=1 CELLPADDING=0 WIDTH=300>
   <TR BGCOLOR="$topAndBottomBG">
      <TD HEIGHT=50>
         <CENTER><FONT SIZE="+3" COLOR="$topAndBottomText"><B><a href=adopt.php?game=$game&act=disown class=white>Disown a Pet</a></B></FONT></CENTER>
      </TD>
   </TR>
</TABLE>
</P>

END;
}

if ($act == "adopt")
{
	$findResults = mysql_query("SELECT * FROM user_pets2 WHERE adoption = '1' AND game = '$game' ORDER BY rand() DESC LIMIT 3");
	$numResults = mysql_num_rows($findResults);
	if ($numResults == 0)
	{
		$itemList .= "<i>There are no pets to adopt right now!</i>";
	}

	if ($numResults != 0)
	{

		while ($getResults = mysql_fetch_array($findResults))
		{
			$findSpecies = fetch("SELECT * FROM pets2 WHERE id = '$getResults[species]' AND game = '$game'");

			$itemList .= "
			  <TD WIDTH=250>
				 <CENTER><a href=adopt.php?game=$game&act=adopt2&id=$getResults[id]>
				 <img src=images/user_images/opg_$game/pets/pet_$findSpecies[id].gif><br>
				 $getResults[name]</a><br>
				 The $findSpecies[name]<br>
			  	 Level: $getResults[level]<br>
			  	 Cost: $getResults[cost2adopt]</CENTER>
			  </TD>
		    ";
		    $x = 1;
		}

		$itemList = "<table><tr bgcolor=#ffffff>$itemList</tr></table>";
	}

ECHO <<<END

         <CENTER><FONT SIZE="+2"><B>--Adopt a Pet--</B></FONT></CENTER>

         <CENTER>$itemList</CENTER>

END;
}

if ($act == "adopt2")
{
	$array = fetch("SELECT * FROM user_pets2 WHERE id = '$id' AND adoption = '1' AND game = '$game'");

	print "<center><table><tr>
	<td width=200>
	";

	$id = $array[id];
	$getPet = fetch("SELECT * FROM pets2 WHERE id = '$array[species]' AND game = '$game'");
	if ($array[hunger] == 0) { $hungerLevel = "Dead"; }
	if ($array[hunger] == 1) { $hungerLevel = "Extremely Starving"; }
	if ($array[hunger] == 2) { $hungerLevel = "Very Starving"; }
	if ($array[hunger] == 3) { $hungerLevel = "Starving"; }
	if ($array[hunger] == 4) { $hungerLevel = "Realllly Hungry"; }
	if ($array[hunger] == 5) { $hungerLevel = "Pretty Hungry"; }
	if ($array[hunger] == 6) { $hungerLevel = "Hungry"; }
	if ($array[hunger] == 7) { $hungerLevel = "Not Hungry"; }
	if ($array[hunger] == 8) { $hungerLevel = "Stuffed"; }
	if ($array[hunger] == 9) { $hungerLevel = "Bloated"; }
	if ($array[hunger] == 10) { $hungerLevel = "Fat"; }
	if (($array[hunger] > 10) OR ($array[hunger] < 0)) { $hungerLevel = "Unknown ($array[hunger])"; }

	$percentHealth = ($array[current_hp] / $array[max_hp]) * 100;
	if ($percentHealth > 0)
	{
		$healthLine = "<font color=red>$array[current_hp]/$array[max_hp]</font>";
	}
	if ($percentHealth > 35)
	{
		$healthLine = "<font color=orange>$array[current_hp]/$array[max_hp]</font>";
	}
	if ($percentHealth > 75)
	{
		$healthLine = "<font color=green>$array[current_hp]/$array[max_hp]</font>";
	}

	echo "
	<p align=center>
	<img src=$base_url/images/user_images/opg_$game/pets/pet_$getPet[id].gif></p>
	</p>
	</td>

	<td width=175>
	<p>
	<b>$array[name]</b> the $getPet[name]!<br>
	Level: $array[level]<br>
	Health: <b>$healthLine</b><br>
	Strength: $array[strength]<br>
	Intelligence: $array[intelligence]<br>
	Speed: $array[speed]<br>
	Defense: $array[defense]<br>
	Hunger: $hungerLevel
	</p>
	</td></tr></table>

	<table>
	<tr>
	<td width=300>

	<p align=center>
	<TABLE BGCOLOR=\"$tableOutline\" CELLSPACING=1 CELLPADDING=0 WIDTH=275>
	<TR BGCOLOR=\"$topAndBottomBG\">
	<TD HEIGHT=30>
	 <CENTER><FONT SIZE=\"+2\" COLOR=\"$topAndBottomText\"><B><a href=adopt.php?game=$game&act=adopt3&id=$id><font color=$topAndBottomText>Adopt for $array[cost2adopt] $pointsVar!</font></a></B></FONT></CENTER>
	</TD>
	</TR>
	</TABLE>

	</td>
	<td>

	<TABLE BGCOLOR=\"$tableOutline\" CELLSPACING=1 CELLPADDING=0 WIDTH=125>
	<TR BGCOLOR=\"$topAndBottomBG\">
	<TD HEIGHT=30>
	 <CENTER><FONT SIZE=\"+2\" COLOR=\"$topAndBottomText\"><B><a href=adopt.php?game=$game&act=adopt><font color=$topAndBottomText>Back</font></a></B></FONT></CENTER>
	</TD>
	</TR>
	</TABLE>
	</td></tr></table></p>";

	print "</center>";
}

if ($act == "disown")
{
	$findPets = mysql_query("SELECT * FROM user_pets2 WHERE owner = '$userid' AND adoption = '0' AND game = '$game' ORDER BY id");

	echo "<p>Please choose the pet you would like to disown:</p><form action=adopt.php?game=$game&act=disown2 method=post><select name=disown><option value=\"\">Choose One";

	while ($array = mysql_fetch_array($findPets))
	{
		$getPet = fetch("SELECT * FROM pets2 WHERE id = '$array[species]' AND game = '$game'");
		print "<option value=$array[id]>$array[name] the $getPet[name] (Level $array[level])";
	}

	print "</select><br><input type=submit value=\"Disown Pet\" onClick=\"if ( !confirm ('Are you sure you want to disown this pet?') ) { return false; }\">";
}

echo $closeHTML;

?>